﻿using UnityEngine;
using System.Collections;

public static class CodingPool
{

    static Color outline = Color.gray;
    static Color hightLight = new Color(0.3f, 0.3f, 1.0f, 1f);
    static Color halfhightLight = new Color(0.86f, 0.86f, 1.0f, 1f);


    static Sprite square;
    public static Sprite GetSquare()
    {
        if (square == null)
        {
            Texture2D sq = new Texture2D(16, 16);
            for (int i = 0; i < sq.width; i++)
                for (int j = 0; j < sq.height; j++)
                {
                    if (i == 0 || i == sq.width - 1
                        || j == 0 || j == sq.height - 1)
                    {
                        sq.SetPixel(i, j, outline);
                    }
                    else
                    {
                        sq.SetPixel(i, j, Color.white);
                    }
                }
            sq.Apply();
            square = Sprite.Create(sq, new Rect(0, 0, sq.width, sq.height), Vector2.one * 0.5f, 80, 0, SpriteMeshType.FullRect, new Vector4(2, 2, 2, 2));
        }
        return square;
    }

    const int max_dropdownSqares_size = 4;
    static Sprite[] dropdownSqares;
    public static Sprite GetDropdownSquare(int size = 2)
    {
        if (dropdownSqares == null)
        {
            dropdownSqares = new Sprite[max_dropdownSqares_size];
            for (int x = 0; x < max_dropdownSqares_size; x++)
            {
                float width = Mathf.Pow(2, x+2) ;
                float height = Mathf.Pow(2, x+2) ;
                Texture2D sq = new Texture2D((int)width, (int)height);
                for (int i = 0; i < sq.width; i++)
                    for (int j = 0; j < sq.height; j++)
                    {
                        if (i == 0 || i == sq.width - 1
                            || j == 0 || j == sq.height - 1
                            
                            
                            )
                        {
                            sq.SetPixel(i, j, outline);
                        }
                        else
                        {
                            sq.SetPixel(i, j, Color.clear);
                        }
                    }
                sq.Apply();

                dropdownSqares[x] = Sprite.Create(sq, new Rect(0, 0, sq.width, sq.height), Vector2.one * 0.5f, width*5, 0, SpriteMeshType.FullRect, new Vector4(2, 2, 2, 2));
            }

            
        }
        return dropdownSqares[size];
    }

    static Sprite[] dropdownHighlightSqares;
    public static Sprite GetDropdownHighlightSquare(int size = 2)
    {
        if (dropdownHighlightSqares == null)
        {
            dropdownHighlightSqares = new Sprite[max_dropdownSqares_size];
            for (int x = 0; x < max_dropdownSqares_size; x++)
            {
                float width = Mathf.Pow(2, x + 2);
                float height = Mathf.Pow(2, x + 2);
                Texture2D sq = new Texture2D((int)width, (int)height);
                for (int i = 0; i < sq.width; i++)
                    for (int j = 0; j < sq.height; j++)
                    {
                        if (i == 0 || i == sq.width - 1
                            || j == 0 || j == sq.height - 1


                            )
                        {
                            sq.SetPixel(i, j, hightLight);
                        }
                        else
                        {
                            sq.SetPixel(i, j, halfhightLight);
                        }
                    }
                sq.Apply();

                dropdownHighlightSqares[x] = Sprite.Create(sq, new Rect(0, 0, sq.width, sq.height), Vector2.one * 0.5f, width * 5, 0, SpriteMeshType.FullRect, new Vector4(2, 2, 2, 2));
            }


        }
        return dropdownHighlightSqares[size];
    }

    static Sprite[] arrows;
    public static Sprite GetArrow(int size = 2)
    {
        if (arrows == null)
        {
            arrows = new Sprite[max_dropdownSqares_size];
            for (int x = 0; x < max_dropdownSqares_size; x++)
            {
                float lineWidth = Mathf.Pow(2, x-1);
                float width = Mathf.Pow(2, x + 3);
                float height = Mathf.Pow(2, x + 2);
                Texture2D sq = new Texture2D((int)width, (int)height);
                sq.wrapMode = TextureWrapMode.Clamp;
                for (int i = 0; i < sq.width; i++)
                    for (int j = 0; j < sq.height; j++)
                    {
                        if (i > 0 && i < sq.width - 1 &&  j < sq.height-1
                            &&(
                            i <=sq.width/2 && Mathf.Abs(i - sq.height + j) < lineWidth
                            || i>sq.width/2 && Mathf.Abs(i-sq.width/2 - j)< lineWidth
                            ))
                        {
                            sq.SetPixel(i, j, outline);
                        }
                        else
                        {
                            sq.SetPixel(i, j, Color.clear);
                        }
                    }
                sq.Apply();

                arrows[x] = Sprite.Create(sq, new Rect(0, 0, sq.width, sq.height), Vector2.one * 0.5f, width * 5, 0, SpriteMeshType.FullRect);
            }


        }
        return arrows[size];
    }


    static Material highlightRevertMat;
    public static Material GetHightlightRevertiMaterial()
    {
        if (highlightRevertMat == null)
        {
            highlightRevertMat = new Material(Shader.Find("UI/RevertX"));
            highlightRevertMat.name = "RevertX mat";
        }
        return highlightRevertMat;
    }

}
